<template>
  <div>
    <el-button type="primary" size="mini">首页</el-button>
    <el-row>
      <el-col :span="8" style="padding-right: 10px">
        <el-card style="margin-bottom: 20px">
          <div class="logo">
            <img src="../assets/img.jpg" />
            <span>
              <h3>Admin</h3>
              <span>超级管理员</span>
            </span>
          </div>
          <div>
            <div class="sign">上次登录时间：<span>2022-12-22</span></div>
            <div class="sign">上次登录地点：<span>广州</span></div>
          </div>
        </el-card>
        <el-card>
          <el-table :data="tableData" style="width: 100%">
            <el-table-column :prop="key" :label="val" v-for="(val, key) in tableLabel" :key="key">
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="16" style="padding-left: 10px">
        <div class="num">
          <el-card v-for="item in countData" :key="item.name" :body-style="{ display: 'flex', padding: 0 }"
            class="box-card">
            <i class="icon" :class="`el-icon-${item.icon}`" :style="{ background: item.color }"></i>
            <div class="detail">
              <p class="price">￥{{ item.value }}</p>
              <p class="dec">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <!-- 折线图 -->
        <el-card style="height: 280px;">
          <div ref="echart" style="height: 280px;">
          </div>
        </el-card>
        <div class="box">
          <el-card style="height: 280px;">
            <div ref="echart2" style="height: 280px;">
            </div>
          </el-card>
          <el-card style="height: 280px;">
            <div ref="echart3" style="height: 260px;">
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getData } from "../api";
import * as echarts from 'echarts';
export default {
  data() {
    return {
      //表格数据
      tableData: [],
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "总购买",
      },
      //订单数据
      countData: [
        {
          name: "今日支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1234,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "本月未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
      ],
    };
  },
  mounted() {
    getData().then((data) => {
      const { orderData, tableData, userData, videoData } = data.data;

      this.tableData = tableData;

      //初始化echarts
      const echart = echarts.init(this.$refs.echart)

      var echartOption = {}

      const xAxis = Object.keys(orderData.data[0])
      const xAxisData = {
        data: xAxis
      }
      echartOption.xAxis = xAxisData
      echartOption.yAxis = {}
      echartOption.legend = xAxisData
      // 触发类型（'item'，数据项图形触发，主要在散点图，饼图等无类目轴的图表中使用；'axis'，坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用；'none'，不触发。
      echartOption.tooltip = {
        trigger: "axis"
      },
        echartOption.series = []
      xAxis.forEach(key => {
        echartOption.series.push({
          name: key,
          data: orderData.data.map(item => item[key]),
          type: 'line'
        })
      })

      echart.setOption(echartOption)

      //柱状图
      const echart2 = echarts.init(this.$refs.echart2)

      var echart2Option = {
        legend: {
          // 图例文字颜色
          textStyle: {
            color: "#333",
          },
        },
        grid: {
          left: "20%",
        },
        // 提示框
        tooltip: {
          trigger: "axis",
        },
        xAxis: {
          type: "category", // 类目轴
          data: userData.map(item => item.date),
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
          axisLabel: {
            interval: 0,
            color: "#333",
          },
        },
        yAxis: [
          {
            type: "value",
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
          },
        ],
        color: ["#2ec7c9", "#b6a2de"],
        series: [
          {
            name: '新增用户',
            data: userData.map(item => item.new),
            type: 'bar'
          },
          {
            name: '活跃用户',
            data: userData.map(item => item.active),
            type: 'bar'
          }
        ],
      }

      echart2.setOption(echart2Option)


      //饼状图

      const echart3 = echarts.init(this.$refs.echart3)

      var echart3Option = {
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: 'value',
          left: 'left'
        },
        color: [
          "#0f78f4",
          "#dd536b",
          "#9462e5",
          "#a6a6a6",
          "#e1bb22",
          "#39c362",
          "#3ed1cf",
        ],
        series: [
          {
            data: videoData,
            type: 'pie',
            radius: ['40%', '70%'],
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 30,
                fontWeight: 'bold'
              }
            },
          }
        ],
      }
      echart3.setOption(echart3Option)
    });

  },
};
</script>

<style lang="less" scoped>
.el-button {
  margin-bottom: 20px;
}

.logo {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ccc;
  margin-bottom: 20px;

  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
    margin: 0 40px 20px 0;
  }

  span {
    h3 {
      font-size: 40px;
      font-weight: 400;
    }

    span {
      margin-top: 20px;
      color: #a8a8a8;
    }
  }
}

.sign {
  font-size: 14px;
  color: #a8a8a8;
  margin-bottom: 10px;

  span {
    margin-left: 70px;
  }
}

.num {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .el-card {
    width: 32%;
    margin-bottom: 20px;
  }

  .icon {
    width: 80px;
    height: 80px;
    font-size: 30px;
    text-align: center;
    line-height: 80px;
    color: #fff;
  }

  .detail {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 20px;

    .price {
      height: 30px;
      font-size: 30px;
      margin-bottom: 10px;
      line-height: 30px;
    }

    .dec {
      font-size: 16px;
      color: #ccc;
    }
  }
}

.box {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;

  .el-card {
    width: 49%;
  }
}
</style>